package com.cxc.visit;



import com.cxc.entity.Apple;
import com.cxc.entity.Banana;
import com.cxc.entity.Fruite;
import com.cxc.entity.Orange;

import java.util.ArrayList;
import java.util.List;

/**
 * 访问者模式：使用了一个访问者类，它改变了元素类的执行算法。通过这种方式，元素的执行算法可以随着访问者改变而改变
 * 主要将数据结构与数据操作分离。
 */
public class Client {

    private static Visit visit = new Visit();
    private static List<Fruite> list = new ArrayList<>();//库存

    static {
        list.add(new Apple(50));
        list.add(new Orange("orange",40));
        list.add(new Banana(60));
    }


    //统计库存
    private static int price(){
        int total = 0;
        for (Fruite fruit : list) {
//            total += visit.sell(fruit);
            total += fruit.accept(visit);
        }
        System.out.println("总价值：" + total);
        return total;
    }

    private static void test(){
        Apple apple = new Apple();
        Fruite f = apple;
        int price  = visit.sell(f);
        System.out.println("价格：："+price);


    }
    private static void test2() {
        Apple apple = new Apple();
        Fruite fruit = apple;
        int price = fruit.accept(visit);
        System.out.println("价格：" + price);
    }

    public static void main(String[] args) {

        price();


    }
}
